package cn.com.chec.en.dao;

import cn.com.chec.en.model.domain.PageLink;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

@Repository
public class PageLinkDao {
	protected static final Logger log = Logger.getLogger(PageLinkDao.class);

	public List<PageLink> getAllAsList() {
		List<PageLink> list = new ArrayList<PageLink>();
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String sql = "select * from tbl_pagelink_i";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			PageLink link = null;
			while (rs.next()) {
				link = new PageLink();
				link.setId(rs.getInt("id"));
				link.setLinkName(rs.getString("link_name"));
				link.setLinkAlia(rs.getString("link_alia"));
				link.setLinkValue(rs.getString("link_value"));
				list.add(link);
			}
			rs.close();
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public PageLink getLinkById(int id) {
		PageLink link = new PageLink();
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String sql = "select * from tbl_pagelink_i where id = ? ";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, id);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				link = new PageLink();
				link.setId(rs.getInt("id"));
				link.setLinkName(rs.getString("link_name"));
				link.setLinkAlia(rs.getString("link_alia"));
				link.setLinkValue(rs.getString("link_value"));
			}
			rs.close();
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return link;
	}

	public boolean updateLinkValuById(int id, String linkValue) {
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String sql = "update tbl_pagelink_i set link_value = ? where id = ? ";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, linkValue);
			pstmt.setInt(2, id);
			pstmt.executeUpdate();
			pstmt.close();
			conn.close();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public Map<String, String> getAllAsMap() {
		Map<String, String> map = new HashMap<String, String>();
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String sql = "select * from tbl_pagelink_i";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				map.put(rs.getString("link_alia"), rs.getString("link_value"));
			}
			rs.close();
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return map;
	}
}